package com.example.bingobox.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.example.bingobox.model.Label;
import java.util.ArrayList;
import java.util.List;

public class TimeSpanSelectionAdapter extends RecyclerView.Adapter<TimeSpanSelectionAdapter.ViewHolder> {
    private List<Label.TimeSpan> timeSpans;
    private List<Label.TimeSpan> selectedTimeSpans = new ArrayList<>();

    public TimeSpanSelectionAdapter(List<Label.TimeSpan> timeSpans) {
        this.timeSpans = timeSpans;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_timespan_selection, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Label.TimeSpan timeSpan = timeSpans.get(position);
        holder.labelText.setText(timeSpan.getLabel());
        holder.timeText.setText(String.format("%02d:%02d-%02d:%02d",
            timeSpan.getStartHour(), timeSpan.getStartMinute(),
            timeSpan.getEndHour(), timeSpan.getEndMinute()));
        
        holder.checkbox.setChecked(selectedTimeSpans.contains(timeSpan));
        holder.itemView.setOnClickListener(v -> {
            holder.checkbox.setChecked(!holder.checkbox.isChecked());
            if (holder.checkbox.isChecked()) {
                selectedTimeSpans.add(timeSpan);
            } else {
                selectedTimeSpans.remove(timeSpan);
            }
        });
    }

    @Override
    public int getItemCount() {
        return timeSpans.size();
    }

    public List<Label.TimeSpan> getSelectedTimeSpans() {
        return selectedTimeSpans;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkbox;
        TextView labelText;
        TextView timeText;

        ViewHolder(View view) {
            super(view);
            checkbox = view.findViewById(R.id.checkbox_timespan);
            labelText = view.findViewById(R.id.text_label);
            timeText = view.findViewById(R.id.text_time);
        }
    }
} 